using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Lib.Helper;
using MSIACCSL2.ViewModels;
using WcfService1.DTO;

namespace MSIACCSL2.Views.Inventory
{
    public partial class AlokasiSaldoAwal : ChildWindow
    {
        public InventoryWarehouseViewModel VM
        {
            get { return (InventoryWarehouseViewModel)this.DataContext; }
        }

        public InventoryWarehouseWrapViewModel VMW
        {
            get { return (InventoryWarehouseWrapViewModel)this.Resources["VMW"]; }
        }
     
        public AlokasiSaldoAwal()
        {
            InitializeComponent();

            EventAggregator.RegisterMessage("RefreshDataContext",(x)=>
                                                                     {
                                                                         foreach (var warehouse in VMW.Warehouses)
                                                                         {
                                                                             foreach (var inventoryWarehouse in VM.InventoryWarehouses)
                                                                             {
                                                                                 if (warehouse.KodeWarehouse == inventoryWarehouse.Warehouse.KodeWarehouse)
                                                                                 {
                                                                                     warehouse.Sheet =
                                                                                        inventoryWarehouse.Sheet;
                                                                                     warehouse.Qty =
                                                                                         inventoryWarehouse.Qty;
                                                                                     continue;
                                                                                 }
                                                                                    
                                                                             }
                                                                         }
                                                                     });
            
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            VM.SaveInventoryWarehouse(null);
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtQty_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).Focus();
            (sender as TextBox).SelectAll();
        }

        private void cboWarehouse_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }

        private void btnAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            VM.AddInventoryWarehouse(VMW.Warehouses);
        }

        private void btnDel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            VM.DeleteInventoryWarehouse((sender as Button).DataContext);
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
    }
}

